<template>
  <basic-dialog-table
    ref="attrDialogRef"
    v-model="visible"
    :apiSource="{
      apiGetList: apiGetlistLinkAttribute,
      apiEdit: apiEditlistLinkAttribute,
      apiDel: apiDellistLinkAttribute,
    }"
    :apiListParams="apiListParams"
    :apiAddParams="apiAddParams"
    :apiEditParams="apiEditParams"
    :apiDelParams="apiDelParams"
    :title="title"
    :hideFooter="false"
    width="80%"
    :option="option"
    delTip="是要移除这个属性吗，注意如果表里面有数据，需要先清空的数据才能移除, 是否继续?"
  >
    <template #menu-left>
      <el-button icon="Plus" type="primary" @click="handleOpenAttrMetaDialog"
        >批量添加属性</el-button
      >
    </template>
  </basic-dialog-table>
  <basic-dialog-table
    ref="attrMetaDialogRef"
    title="从属性池里选择属性"
    selectType="multiple"
    v-model="visibleAttrMeta"
    :apiSource="{
      apiGetList: apiGetAttributeDialogList,
    }"
    :apiListParams="apiListParamsAttrMeta"
    :option="optionAttrMeta"
    :submitBtnLoading="submitBtnLoadingAttrMeta"
    @submit="handleSubmitAttrMeta"
  >
  </basic-dialog-table>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import {
  apiGetlistLinkAttribute,
  apiEditlistLinkAttribute,
  apiDellistLinkAttribute,
  apiAddlistLinkAttribute,
} from '@/apis/objectModel/businessType'
import { apiGetAttributeDialogList } from '@/apis/objectModel/attribute.js'
const visible = defineModel()
const option = {
  addBtn: false,
  refreshBtn: true,
  height: '45vh',
  searchLabelWidth: 140,
  searchSpan: 12,
  page: false,
  menuWidth: 180,
  group: [
    {
      label: '属性基本信息',
      prop: 'baseInfo',
      column: [
        {
          label: '属性英文名称',
          prop: 'code',
          search: true,
          width: 150,
          overHidden: true,
          disabled: true,
          placeholder: '从属性池里选择属性',
          sortable: true,
          rules: [
            {
              required: true,
              message: '属性英文名称不能为空',
              trigger: 'blur',
            },
          ],
        },
        {
          label: '属性中文名称',
          prop: 'name',
          disabled: true,
        },
        {
          label: '对象里属性',
          prop: 'objCode',
          disabled: true,
        },
        {
          label: '属性类型',
          prop: 'attributeDataType',
          disabled: true,
          type: 'select',
          dicData: [
            {
              label: '字符串',
              value: 'String',
            },
            {
              label: '整形',
              value: 'Integer',
            },
            {
              label: '长整形',
              value: 'Long',
            },
            {
              label: '金额/双精度',
              value: 'Double',
            },
            {
              label: '开关/是否',
              value: 'Boolean',
            },
            {
              label: '日期',
              value: 'Date',
            },
            {
              label: '日期时间',
              value: 'DateTime',
            },
            {
              label: '时间',
              value: 'Time',
            },
            {
              label: '超长文本',
              value: 'Clob',
            },
            {
              label: '文件',
              value: 'File',
            },
          ],
        },
        {
          label: '可空',
          prop: 'nullableFlag',
          type: 'switch',
          disabled: true,
          dicData: [
            {
              label: '是',
              value: 'true',
            },
            {
              label: '否',
              value: 'false',
            },
          ],
        },
        {
          label: '默认值',
          prop: 'defaultValue',
          disabled: true,
          width: 100,
        },
        {
          label: '枚举类型',
          prop: 'enumCode',
          disabled: true,
          formatter: (val) => {
            return (
              (val.enumCode == null ? '' : val.enumCode) +
              ' ' +
              (val.enumName == null ? '' : val.enumName)
            )
          },
        },
        {
          label: '引用类型',
          prop: 'busCode',
          disabled: true,
          formatter: (val) => {
            return (
              (val.busCode == null ? '' : val.busCode) +
              ' ' +
              (val.busReferName == null ? '' : val.busReferName)
            )
          },
        },
        {
          label: '描述',
          disabled: true,
          prop: 'des',
        },
      ],
    },
    {
      label: '当前业务类型/关联类型里配置',
      prop: 'thisObj',
      column: [
        {
          label: '属性显示名称',
          placeholder:
            '属性在当前对象里显示的名称，可空，为空时默认为属性中文名称',
          prop: 'showName',
        },
        {
          label: '长度',
          prop: 'attributeLength',
          type: 'number',
          width: 90,
          step: 10,
          value: 50,
          tip: '整形，长整形，超长文本，开关类型的时候，后台会自动默认长度，页面输入的无效',
          clearable: true,
        },
        {
          label: '小数精度',
          prop: 'precisionLength',
          type: 'number',
          width: 90,
          tip: '金额/双精度的时候，小数精度必须要填写，否则系统默认为2',
          clearable: true,
        },
        {
          label: '索引',
          prop: 'indexFlag',
          type: 'switch',
          value: 'false',
          dicData: [
            {
              label: '否',
              value: 'false',
            },
            {
              label: '是',
              value: 'true',
            },
          ],
        },
        {
          label: '组合索引的字段',
          prop: 'indexField',
          placeholder: '请输入属性的英文名称，以英文的逗号分隔',
        },
      ],
    },
  ],
  column: [
    {
      label: '属性英文名称',
      prop: 'code',
      search: true,
      width: 150,
      display: false,
      overHidden: true,
    },
    {
      label: '属性中文名称',
      prop: 'name',
      width: 180,
      search: true,
      overHidden: true,
      display: false,
    },
    {
      label: '显示名称',
      prop: 'showName',
      width: 180,
      search: true,
      display: false,
      overHidden: true,
    },
    {
      label: '对象里属性',
      prop: 'objCode',
      width: 120,
      display: false,
      overHidden: true,
    },
    {
      label: '属性类型',
      prop: 'attributeDataType',
      width: 100,
      display: false,
      overHidden: true,
      type: 'select',
      dicData: [
        {
          label: '字符串',
          value: 'String',
        },
        {
          label: '整形',
          value: 'Integer',
        },
        {
          label: '长整形',
          value: 'Long',
        },
        {
          label: '金额/双精度',
          value: 'Double',
        },
        {
          label: '开关/是否',
          value: 'Boolean',
        },
        {
          label: '日期',
          value: 'Date',
        },
        {
          label: '日期时间',
          value: 'DateTime',
        },
        {
          label: '时间',
          value: 'Time',
        },
        {
          label: '超长文本',
          value: 'Clob',
        },
        {
          label: '文件',
          value: 'File',
        },
      ],
    },
    {
      label: '索引',
      prop: 'indexFlag',
      type: 'switch',
      value: 'true',
      display: false,
      overHidden: true,
      width: 80,
      dicData: [
        {
          label: '是',
          value: 'true',
        },
        {
          label: '否',
          value: 'false',
        },
      ],
    },
    {
      label: '组合索引的字段',
      prop: 'indexField',
      display: false,
      overHidden: true,
      width: 140,
    },
    {
      label: '默认属性',
      prop: 'defaultAttr',
      type: 'switch',
      value: 'true',
      display: false,
      overHidden: true,
      width: 100,
      dicData: [
        {
          label: '是',
          value: 'true',
        },
        {
          label: '否',
          value: 'false',
        },
      ],
    },
    {
      label: '系统属性',
      prop: 'systemAttr',
      type: 'switch',
      value: 'true',
      display: false,
      overHidden: true,
      width: 100,
      dicData: [
        {
          label: '是',
          value: 'true',
        },
        {
          label: '否',
          value: 'false',
        },
      ],
    },
    {
      label: '可空',
      prop: 'nullableFlag',
      type: 'switch',
      value: 'true',
      display: false,
      overHidden: true,
      width: 80,
      dicData: [
        {
          label: '是',
          value: 'true',
        },
        {
          label: '否',
          value: 'false',
        },
      ],
    },
    {
      label: '默认值',
      prop: 'defaultValue',
      display: false,
      overHidden: true,
      width: 100,
    },
    {
      label: '枚举类型',
      prop: 'enumName',
      display: false,
      overHidden: true,
      width: 160,
      formatter: (val) => {
        return (
          (val.enumCode == null ? '' : val.enumCode) +
          ' ' +
          (val.enumName == null ? '' : val.enumName)
        )
      },
    },
    {
      label: '引用类型',
      prop: 'busReferName',
      width: 160,
      formatter: (val) => {
        return (
          (val.busCode == null ? '' : val.busCode) +
          ' ' +
          (val.busReferName == null ? '' : val.busReferName)
        )
      },
      display: false,
      overHidden: true,
    },
    {
      label: '长度',
      prop: 'attributeLength',
      type: 'number',
      width: 90,
      display: false,
      overHidden: true,
    },
    {
      label: '小数精度',
      prop: 'precisionLength',
      type: 'number',
      width: 90,
      display: false,
      overHidden: true,
    },
    {
      label: '描述',
      prop: 'des',
      display: false,
      overHidden: true,
    },
  ],
}
const props = defineProps({
  apiListParams: Object,
  apiAddParams: Object,
  apiEditParams: Object,
  apiDelParams: Object,
  curRow: Object,
  title: String,
})
const attrDialogRef = ref()
const visibleAttrMeta = ref(false)
const apiListParamsAttrMeta = reactive({
  notCode: '',
})
const optionAttrMeta = {
  height: '50vh',
  menu: false,
  header: false,
  column: [
    {
      label: '编号',
      prop: 'code',
      search: true,
    },
    {
      label: '名称',
      prop: 'name',
      search: true,
    },
  ],
}
const submitBtnLoadingAttrMeta = ref(false)
const attrMetaDialogRef = ref([])
const handleOpenAttrMetaDialog = () => {
  visibleAttrMeta.value = true
  apiListParamsAttrMeta.notCode = attrDialogRef.value.basicCrudRef.tableData
    .map((val) => val.code)
    .join(',')
}
const handleSubmitAttrMeta = async (resData) => {
  resData = resData.map((item) => {
    ;(item.sourceId = props.curRow.id), (item.sourceBus = props.curRow.bus)
    return item
  })
  await apiAddlistLinkAttribute(resData, submitBtnLoadingAttrMeta)
  ElMessage.success('新增成功！')
  attrDialogRef.value.basicCrudRef.getList()
  visibleAttrMeta.value = false
}
</script>

<style lang="scss" scoped></style>
